#import "OmRegisterVC.h"
#import "OmRegisterVCDatePickerView.h"
#import "OmRegisterVCSexChooseView.h"
#import "OmRegisterVCCharacterView.h"
@interface OmRegisterVC ()
@property (nonatomic, strong) UITextField *accountField;
@property (nonatomic, strong) UIView *lineForAccountField;
@property (nonatomic, strong) UITextField *mailField;
@property (nonatomic, strong) UIView *lineForMailField;
@property (nonatomic, strong) UITextField *passwordField;
@property (nonatomic, strong) UIView *lineForPasswordField;
@property (nonatomic, strong) UITextField *checkPasswordField;
@property (nonatomic, strong) UIView *lineForCheckPasswordField;
@property (nonatomic, strong) OmBaseButton *birthDayButton;
@property (nonatomic, strong) UIView *lineForBirthDayButton;
@property (nonatomic, strong) UIButton *submitButton;
@property (nonatomic, strong) OmRegisterVCDatePickerView *birthDayPickerView;
@property (nonatomic, strong) OmRegisterVCSexChooseView *sexChooseView;
@property (nonatomic, strong) OmRegisterVCCharacterView *characterView;
@end
@implementation OmRegisterVC
#pragma mark - Events
- (void)onBirthDayButtonTap {
    self.birthDayPickerView.hidden = NO;
}
- (void)dateChange:(UIDatePicker *)datePicker {
    NSDate *theDate = datePicker.date;
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateFormat = @"YYYY-MM-dd";
    self.birthDayButton.coolLabel.text = [dateFormatter stringFromDate:theDate];
}
- (void)onSubmitButtonTap {
    if ([self isRegisterInformationCorrect]) {
        NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
        parameters[@"name"] = self.accountField.text;
        parameters[@"email"] = self.mailField.text;
        parameters[@"pwd"] = self.passwordField.text;
        parameters[@"birthday"] = @([self timeStampFromBirthDay]);
        parameters[@"sex"] = self.sexChooseView.sex;
        parameters[@"character"] = self.characterView.characters;
        [[HUDHelper sharedInstance] syncLoading:ASLocalizedString(@"注册中")];
        [userManager login:OmUserLoginTypeRegister params:parameters completion:^(BOOL success, NSString *des) {
            if(success) {
                [[HUDHelper sharedInstance] syncStopLoading];
                KPostNotification(KNotificationLoginStateChange, @YES);
            }
        }];
    }
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [[[UIApplication sharedApplication] keyWindow] endEditing:YES];
}
#pragma mark - Method
- (BOOL)isRegisterInformationCorrect {
    if (self.accountField.text.length <= 0) {
        [MBProgressHUD showTipMessageInView:ASLocalizedString(@"请输入用户名")];
        return NO;
    }
    if (self.mailField.text.length <= 0) {
        [MBProgressHUD showTipMessageInView:ASLocalizedString(@"请输入邮箱")];
        return NO;
    }
    if (self.passwordField.text.length <= 0) {
        [MBProgressHUD showTipMessageInView:ASLocalizedString(@"请输入密码")];
        return NO;
    }
    if ([self.birthDayButton.coolLabel.text isEqualToString:ASLocalizedString(@"请选择生日")]) {
        [MBProgressHUD showTipMessageInView:ASLocalizedString(@"请选择生日")];
        return NO;
    }
    if (self.sexChooseView.sex.length <= 0) {
        [MBProgressHUD showTipMessageInView:ASLocalizedString(@"请选择性别")];
        return NO;
    }
    if (![self.passwordField.text isEqualToString:self.checkPasswordField.text]) {
        [MBProgressHUD showTipMessageInView:ASLocalizedString(@"密码验证不正确")];
        return NO;
    }
    return YES;
}
- (NSInteger)timeStampFromBirthDay {
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateFormat = @"YYYY-MM-dd";
    NSDate *date = [dateFormatter dateFromString:self.birthDayButton.coolLabel.text];
    return [date timeIntervalSince1970];
}
#pragma mark - Construction Script
- (void)configSelf {
    self.title = ASLocalizedString(@"账号注册");
    [self setHiddenForTabBar:YES navigationBar:NO];
}
- (void)allocateComponents {
    self.accountField = [[UITextField alloc] init];
    [self.accountField setFont:RPXFont(15)];
    self.accountField.placeholder = ASLocalizedString(@"请输入用户名");
    [self.view addSubview:self.accountField];
    self.lineForAccountField = [[UIView alloc] init].fp_backgroundColor(HexadecimalColor(@"#F07896"));
    [self.view addSubview:self.lineForAccountField];
    self.mailField = [[UITextField alloc] init];
    [self.mailField setFont:RPXFont(15)];
    self.mailField.placeholder = ASLocalizedString(@"请输入邮箱");
    [self.view addSubview:self.mailField];
    self.lineForMailField = [[UIView alloc] init].fp_backgroundColor(HexadecimalColor(@"#F07896"));
    [self.view addSubview:self.lineForMailField];
    self.passwordField = [[UITextField alloc] init];
    [self.passwordField setFont:RPXFont(15)];
    self.passwordField.placeholder = ASLocalizedString(@"请输入密码");
    [self.view addSubview:self.passwordField];
    self.lineForPasswordField = [[UIView alloc] init].fp_backgroundColor(HexadecimalColor(@"#F07896"));
    [self.view addSubview:self.lineForPasswordField];
    self.checkPasswordField = [[UITextField alloc] init];
    [self.checkPasswordField setFont:RPXFont(15)];
    self.checkPasswordField.placeholder = ASLocalizedString(@"请确认密码");
    [self.view addSubview:self.checkPasswordField];
    self.lineForCheckPasswordField = [[UIView alloc] init].fp_backgroundColor(HexadecimalColor(@"#F07896"));
    [self.view addSubview:self.lineForCheckPasswordField];
    self.birthDayButton = [[OmBaseButton alloc] initWithImageName:@"OmDownIndex"
                                                        imageSize:CGSizeMake(RPX(13), RPX(7))
                                               imageRightDistance:0
                                                            title:ASLocalizedString(@"请选择生日")
                                                        titleFont:RPXFont(15)
                                                       titleColor:HexadecimalColor(@"#C7C7CC")
                                                titleLeftDistance:0];
    [self.birthDayButton addTarget:self action:@selector(onBirthDayButtonTap) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.birthDayButton];
    self.lineForBirthDayButton = [[UIView alloc] init].fp_backgroundColor(HexadecimalColor(@"#F07896"));
    [self.view addSubview:self.lineForBirthDayButton];
    self.sexChooseView = [[OmRegisterVCSexChooseView alloc] init];
    [self.view addSubview:self.sexChooseView];
    self.characterView = [[OmRegisterVCCharacterView alloc] init];
    [self.view addSubview:self.characterView];
    self.submitButton = [[UIButton alloc] init].fp_addTargetActionForControlEvents(self, @selector(onSubmitButtonTap), UIControlEventTouchUpInside);
    [self.submitButton setBackgroundImage:IMAGE_NAMED(@"OmButton0") forState:UIControlStateNormal];
    [self.submitButton setTitle:ASLocalizedString(@"完成") forState:UIControlStateNormal];
    [self.submitButton setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, RPX(22), 0)];
    self.submitButton.titleLabel.font = RPXFont(14);
    [self.view addSubview:self.submitButton];
    self.birthDayPickerView = [[OmRegisterVCDatePickerView alloc] init];
    [self.birthDayPickerView.datePicker addTarget:self action:@selector(dateChange:) forControlEvents:UIControlEventValueChanged];
    self.birthDayPickerView.hidden = YES;
    [self.view addSubview:self.birthDayPickerView];
}
- (void)layoutComponents {
    [self.accountField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(RPX(30));
        make.right.equalTo(self.view).offset(RPX(-30));
        make.top.equalTo(self.view).offset(RPX(32));
    }];
    [self.lineForAccountField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.accountField.mas_bottom).offset(RPX(13));
        make.centerX.equalTo(self.view);
        make.width.equalTo(@(RPX(345)));
        make.height.equalTo(@(1));
    }];
    [self.mailField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(RPX(30));
        make.right.equalTo(self.view).offset(RPX(-30));
        make.top.equalTo(self.lineForAccountField.mas_bottom).offset(RPX(27));
    }];
    [self.lineForMailField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mailField.mas_bottom).offset(RPX(13));
        make.centerX.equalTo(self.view);
        make.width.equalTo(@(RPX(345)));
        make.height.equalTo(@(1));
    }];
    [self.passwordField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(RPX(30));
        make.right.equalTo(self.view).offset(RPX(-30));
        make.top.equalTo(self.lineForMailField.mas_bottom).offset(RPX(27));
    }];
    [self.lineForPasswordField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.passwordField.mas_bottom).offset(RPX(13));
        make.centerX.equalTo(self.view);
        make.width.equalTo(@(RPX(345)));
        make.height.equalTo(@(1));
    }];
    [self.checkPasswordField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(RPX(30));
        make.right.equalTo(self.view).offset(RPX(-30));
        make.top.equalTo(self.lineForPasswordField.mas_bottom).offset(RPX(27));
    }];
    [self.lineForCheckPasswordField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.checkPasswordField.mas_bottom).offset(RPX(13));
        make.centerX.equalTo(self.view);
        make.width.equalTo(@(RPX(345)));
        make.height.equalTo(@(1));
    }];
    [self.birthDayButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.lineForCheckPasswordField.mas_bottom).offset(RPX(13));
        make.left.equalTo(self.view).offset(RPX(30));
        make.right.equalTo(self.view).offset(RPX(-30));
        make.height.equalTo(@(RPX(43)));
    }];
    [self.lineForBirthDayButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.birthDayButton.mas_bottom);
        make.centerX.equalTo(self.view);
        make.width.equalTo(@(RPX(345)));
        make.height.equalTo(@(1));
    }];
    [self.sexChooseView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.lineForBirthDayButton.mas_bottom);
        make.left.equalTo(self.view);
    }];
    [self.characterView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.sexChooseView.mas_bottom);
        make.left.equalTo(self.view);
    }];
    [self.submitButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(RPX(529));
        make.centerX.equalTo(self.view);
        make.width.equalTo(@(RPX(180)));
        make.height.equalTo(@(RPX(66)));
    }];
    [self.birthDayPickerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.equalTo(self.view);
    }];
}
@end
